package com.dnxh.util.component;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.dnxh.entity.BasicInfo;
import com.dnxh.entity.Meeting;
import com.dnxh.service.BasicInfoService;
import com.dnxh.service.MeetingService;
import com.dnxh.util.EmailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 枳湫
 * @Time 2022/4/12 23:33
 * @Describe Basic_jar
 **/
@Slf4j
@Component
public class MeetingUtil {
	
	@Lazy
	@Resource
	private MeetingService   metingService;
	@Resource
	private BasicInfoService basicInfoService;
	@Resource
	private EmailUtil        emailUtil;
	
	@Async ("meeting-type->")
	public void sendMeetingEmail (Long meetingId, List<String> grade, List<String> departments) {
		try {
			Thread.sleep (2000);
			Meeting meeting = metingService.getById (meetingId);
			List<BasicInfo> gradeList = basicInfoService.searchByGrade (grade);
			List<BasicInfo> department = basicInfoService.searchByDepartment (departments);
			List<BasicInfo> list = new ArrayList<> (gradeList.size () + department.size ());
			if (CollUtil.isEmpty (grade)) {
				list = department;
			} else if (CollUtil.isEmpty (departments)) {
				list = gradeList;
			} else {
				for (BasicInfo info : department) {
					if (grade.contains (info.getGrade ())) {
						list.add (info);
					}
				}
			}
			list.forEach (info ->
					emailUtil.sendMatchNoticeEmail (info.getEmail (), info.getNickName (), meeting.getTime (),
							meeting.getLocation (), meeting.getMName (), StrUtil.EMPTY)
			);
		} catch (InterruptedException e) {
			e.printStackTrace ();
		}
	}
}
